package web;

import utils.JDBCutils;

import javax.servlet.*;
import java.io.IOException;

public class TransationFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {


        try {
            filterChain.doFilter(servletRequest,servletResponse);
            JDBCutils.closeAndCommit();
            System.out.println("我提交了事务");
        } catch (Exception e) {
            JDBCutils.rollBack();
            System.out.println("我回滚了事务");
            e.printStackTrace();
            //将所有异常都统一交给Tomcat，让Tomcat展示友好的错误信息页面
            /*
            * 在web.xml中我们可以通过错误页面配置来进行管理
            * */
            throw new RuntimeException(e);
        }


    }
}
